##*****************************************************************************
## FIGURE A1
##*****************************************************************************
## 
## R Version: R version 3.2.4 (2016-03-1)
##

##LOADING PACKAGES
library(ggplot2) ##Version: 2.1.0
library(leiv)

##SET WORKING DIRECTORY
setwd('~/workspace/fed_courts/scaling_paper/')

##LOADING DATASET
fjc <- read.csv('replication_materials/data/bonica_sen_fed_judges.csv')

##RESCALING JCS SCORES
lm.out <- leiv(dime.cfscore~jcs.score.dw,data=fjc,abs.tol=1e-10)
fjc$jcs.score.dw <- lm.out@intercept + lm.out@slope*fjc$jcs.score.dw


fjc <- fjc[rev(order(fjc$enter.year)),]
fjc$exit.year[is.na(fjc$exit.year)] <- 3000 


syear = 1990
cm2 <- cm <- NULL
for(x in syear:2014){
    u1 <- which(grepl('Court of App',fjc$court.name,ignore.case=T) & fjc$enter.year <= x &  fjc$exit.year > x)
    u2 <- which(grepl('Court of App',fjc$court.name1,ignore.case=T) & fjc$enter.year1 <= x & fjc$exit.year1 > x)
    u3 <- which(grepl('Court of App',fjc$court.name2,ignore.case=T) & fjc$enter.year2 <= x & fjc$exit.year2 > x)
    uu1 <- fjc[u1,]
    uu2 <- fjc[u2,]
    uu2$court.name <- uu2$court.name1
    uu3 <- fjc[u3,]
    uu3$court.name <- uu3$court.name2


    fjc3 = rbind(uu1,uu2,uu3)
    fjc3$circuit <- factor(strwrap(gsub('U. S. Court of Appeals for the','',fjc3$court.name,ignore.case=T)))
    pp1 <- with(fjc3,paste(fjc.judge.idno,circuit,sep='_'))

    qq=duplicated(with(fjc3,paste(fjc.judge.idno,circuit,sep='_')))
    fjc3 <- fjc3[!qq,]


    fjc3$circuit <- factor(strwrap(gsub('U. S. Court of Appeals for the','',fjc3$court.name,ignore.case=T)))
    fjc3 <- fjc3[!grepl('federal circuit',fjc3$court.name,ignore.case=T),]
    fjc3$circuit2  <- as.character(fjc3$circuit)


    print( with(fjc3,aggregate(imputed.dime.cfscore,list(circuit2),length)))
    aa1 <- with(fjc3,aggregate(imputed.dime.cfscore,list(circuit2),median,na.rm=T))
    if(x == syear){
        cm <- aa1
    }else{
        cm <- cbind(cm,aa1[match(cm[,1],aa1[,1]),2])
    }
    aa2 <- with(fjc3,aggregate(jcs.score.dw,list(circuit2),median,na.rm=T))
    if(x == syear){
        cm2 <- aa2
    }else{
        cm2 <- cbind(cm2,aa2[match(cm2[,1],aa2[,1]),2])
    }

}

colnames(cm) <- c('Circuit',syear:2014)
cm <- cm[!is.na(rownames(cm)),]

colnames(cm2) <- c('Circuit',syear:2014)
cm2 <- cm2[!is.na(rownames(cm2)),]


gg <- NULL
for(x in 1:nrow(cm)){
    gg <- rbind(gg,
                cbind(cm[x,1],t(cm[x,-1]),syear:2014,'DIME'),
                cbind(cm[x,1],t(cm2[x,-1]),syear:2014,'JCS'))
}
gg <- data.frame(gg)

co <- c('First Circuit',
        'Second Circuit',
        'Third Circuit',
        'Fourth Circuit',
        'Fifth Circuit',
        'Sixth Circuit',
        'Seventh Circuit',
        'Eighth Circuit',
        'Ninth Circuit',
        'Tenth Circuit',
        'Eleventh Circuit',
        'District of Columbia Circuit')


gg[,1] <- factor(as.character(gg[,1]),co)
gg[,2] <- as.numeric(as.character(gg[,2]))
gg[,3] <- as.numeric(as.character(gg[,3]))
colnames(gg) <- c('st','mip','year','type')
gg <- gg[!is.na(gg[,1]),]

p1 <- qplot(x=year,y=mip,data=gg,geom='line',linetype=type)
p1 <- p1+ylab('Circuit Median')
p1 <- p1 + xlab('Year')
p1 <- p1 + facet_wrap(~st)
p1 <- p1 + theme_bw()
p1 <- p1 + ylim(-1.25,1.25)
p1 <- p1 + theme(axis.title=element_text(size=12))
p1 <- p1 + scale_linetype('')
p1 <- p1 + scale_x_continuous('', breaks=seq(1992,2012,4), labels=seq(1992,2012,4),limits=c(1990,2014))

pdf(file='replication_materials/figures/figure_A2.pdf',width = 12,height= 8)
print(p1)
dev.off()

